//Copyright (C) 2012 Chirila Alexandru
//This file is part of Mirror FTP
//
//Mirror FTP is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.
//
//Mirror FTP is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with Mirror FTP.  If not, see <http://www.gnu.org/licenses/>.

#ifndef FTP_H_
#define FTP_H_

//#include <pthread.h>

//#include <queue>

//#include <iostream>
//#include <fstream>
//#include <string>
//#include <sstream>

#include "xml/pugixml.hpp"

#include "conn_cmd.h"
#include "conn_dta.h"
#include "filenode.h"
//#include "model.h"

extern int h_errno;

using namespace std;
/**
 * Manages both the control and data connection and therefore the
 * whole ftp connection
 */
class ftp : public model{
public:
	ftp();
	~ftp();
private:
	string addrServer;		///<current server address
	struct hostent host;	///<resolved host
	conn_cmd cmd;			///<control connection
	conn_dta dta;			///<data connection
	file_node *fl;			///<file node structure (graph)
	queue<file_node*> Q;	///<queue for pending downloads

	int c_port;				///<current used port
	string c_user;			///<current used username
	string c_pass;			///<current used password

	bool stop;				///<flag for stoping any ongoing activity
	int refr;				///<flag for starting refreshing procedure
	int refr_rate;			///<refresh rate (seconds)
	int time_out;			///<time out rate (miliseconds)
	unsigned long int thread;///<thread id

	string welcome;			///<welcome message;
	string server_address;	///<resolved ip of server
public:
	static void *timer(void *arg);

	string get_welcome(){return welcome;};
	string get_server_address(){return server_address;};
	void set_address(string x){addrServer=x;};
	string get_address(){return addrServer;};
	void set_refresh_rate(int x){refr_rate=x;};
	int get_refresh_rate(){return ftp::refr_rate;};
	void set_time_out(int x){time_out=x;};
	int get_time_out(){return time_out;};
	int get_c_port(){return c_port;};
	string get_c_user(){return c_user;};
	string get_c_pass(){return c_pass;};

	int resolve();
	int connect(int port);
	int login(string u,string p);

	int send_receive(char data_mode,long long size,string command,string argument,string file,char data_type);
	int send (string c,string p,bool b_notify=1);
	string get_last(){return cmd.get_last_resp();};

	void download_all(char data_mode,char list_mode,string path);
	void refresh_all(char);

	void empty_queue();
	void dump_to_xml(string);
	bool check_completed(){return fl==NULL;};
	int open_from_xml(string);

	void cancel();
	int closeAll();

	int is_connected(){return cmd.connected();};

private:
	void xml_node_proc(pugi::xml_node,file_node*);
	int connect_dta_active();
	int connect_dta_passive();
	int analyse_normal(file_node *node,char *);
	int analyse_machine(file_node *node,char *);
};

#endif /* FTP_H_ */
